package com.gainloop;

import java.io.IOException;
import java.net.MalformedURLException;
import java.net.URL;
import java.util.ArrayList;

import org.alexd.jsonrpc.JSONRPCException;

import android.app.ListActivity;
import android.app.ProgressDialog;
import android.content.Context;
import android.content.Intent;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.os.Bundle;
import android.os.Looper;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView.*;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.Toast;


import com.gainloop.R;
import com.gainloop.Adapters.HelpAdapter;
import com.gainloop.Api.ApiAbstract;
import com.gainloop.Api.ApiResponseObject;
import com.gainloop.Api.Helps;
import com.gainloop.Api.Users;
import com.gainloop.Api.Wishes;
import com.gainloop.Api.Entity.EntityAbstract;
import com.gainloop.Api.Entity.Help;
import com.gainloop.Api.Entity.User;
import com.gainloop.Api.Entity.Wish;
import com.gainloop.Utils.Constants;
import com.gainloop.Utils.Constants.ListType;

public class HelpsList extends ListActivity {
	private ArrayList<Help> helps = null;
	private HelpAdapter adapter;
	private Runnable viewHelps;
	private ProgressDialog progressDialog = null;
	private ListType listType;
	private EntityAbstract entity;
	
	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.wish_list);
		adapter = new HelpAdapter(this, R.layout.wish_item, new ArrayList<Help>());
		setListAdapter(adapter);
		//listType = (ListType)getIntent().getExtras().get(Constants.listTypeParam);
		if (getIntent().hasExtra(Constants.wishParam)){
			getEntity(Constants.wishParam,(Long)getIntent().getExtras().get(Constants.wishParam));
		}
		else{
			getEntity(Constants.userParam,(Long)getIntent().getExtras().get(Constants.userParam));
		}
		//Toast.makeText(HelpsList.this, "user is: "+entity.getName(), Toast.LENGTH_SHORT).show();

		viewHelps = new Runnable() {
			@Override
			public void run() {
				getHelps();
			}
		};
		Thread thread = new Thread(null, viewHelps, "MagentoBackground");


		progressDialog = ProgressDialog.show(HelpsList.this,getString(R.string.wait_msg),
				getString(R.string.ret_data_msg), true);
		thread.start();

		ListView lv = getListView();
		lv.setTextFilterEnabled(true);
		/*
		lv.setOnItemClickListener(new OnItemClickListener() {
			public void onItemClick(AdapterView<?> parent, View view,
					int position, long id) {
				Help help=adapter.getItem(position);
				
				Toast.makeText(getApplicationContext(), "Wish Id: "+wish.getId(),
						Toast.LENGTH_SHORT).show();

				Intent intent=new Intent(HelpsList.this,GainPage.class)
					.putExtra(GainPage.GAIN_ID, wish.getId());
				HelpsList.this.startActivity(intent);
				
			}

			
		});*/

	}
	private void getEntity (String entType, Long id){
		ApiResponseObject<? extends EntityAbstract> resp=null;

		try {
				if (entType.compareTo(Constants.wishParam)==0){
					
					Wishes wishes=new Wishes();
					resp=wishes.getById(id);
					
				}
				else{
					
					Users users=new Users();
					resp=users.getById(id);
				}
				if (resp.getReturnCode()==0){
					entity=resp.getData();
				}	
				else{
					//could not get user from server.
					throw new RuntimeException(""+R.string.server_error+ resp.getReturnCode());
				}
				
			
		} catch (JSONRPCException e) {
			e.printStackTrace();
			throw new RuntimeException();
		}
		
	}
	private void getHelps() {
		try {
			
			//Toast.makeText(HelpsList.this,"getting the helps..", Toast.LENGTH_SHORT).show();

			if  (getIntent().hasExtra(Constants.wishParam)){
				helps = Helps.getInstance().getAllByWish((Wish) entity, 1);
			}
			else{
				helps = Helps.getInstance().getAllByUser((User)entity);
			}
			//}
			//Toast.makeText(HelpsList.this, "helps found: "+helps.size(), Toast.LENGTH_SHORT).show();
			
		} catch (JSONRPCException e) {
			e.printStackTrace();
			throw new RuntimeException();
		}
		runOnUiThread(returnRes);
	}
/*
	private class HelpAdapter extends ArrayAdapter<Help> {

		private ArrayList<Help> items;

		public HelpAdapter(Context context, int textViewResourceId,
				ArrayList<Help> items) {
			super(context, textViewResourceId, items);
			this.items = items;
		}

		@Override
		public View getView(int position, View convertView, ViewGroup parent) {
			View v = convertView;
			if (v == null) {
				LayoutInflater vi = (LayoutInflater) getSystemService(Context.LAYOUT_INFLATER_SERVICE);
				v = vi.inflate(R.layout.wish_item, null);
			}
			Help item = items.get(position);
			if (item != null) {
				ImageView profile = (ImageView) v
						.findViewById(R.id.wishItemProfileImg);
				TextView content = (TextView) v
						.findViewById(R.id.wishItemContent), username = (TextView) v
						.findViewById(R.id.wishItemUsername), category = (TextView) v
						.findViewById(R.id.wishItemCategory), helps = (TextView) v
						.findViewById(R.id.wishItemHelps);

				try {
					URL newurl = new URL(
							"http://graph.facebook.com/" + item.getUserId() + "/picture?type=square");
					Bitmap bitmap = BitmapFactory.decodeStream(newurl
							.openConnection().getInputStream());
					profile.setImageBitmap(bitmap);
				} catch (MalformedURLException e) {
					e.printStackTrace();
				} catch (IOException e) {
					e.printStackTrace();
				}
				
				username.setText(""+item.getUserId());
				content.setText(item.getContent());
				//category.setText(item.getCategory());
				//helps.setText("" + item.getHelpsNum());
			}
			return v;
		}
	}
*/
	private Runnable returnRes = new Runnable() {

		@Override
		public void run() {
			//Toast.makeText(HelpsList.this, "helps found: "+helps.size(), Toast.LENGTH_SHORT).show();

			if (helps != null && helps.size() > 0) {
				adapter.notifyDataSetChanged();
				
				for (int i = 0; i < helps.size(); i++){
					adapter.add(helps.get(i));
				}
			}
			progressDialog.dismiss();
			adapter.notifyDataSetChanged();
		}
	};

}